<template>
   <div id='detail' style="touch-action: none">
    <detail-nav-bar class='detail-nav' @titleClick = 'titleClick' ref='nav'></detail-nav-bar> 
     <scroll class="content" ref = 'scroll' :probe-type = '3' @scroll = 'contentScroll' >  
      <detail-swiper :top-images = 'topImages'></detail-swiper>
      <detail-baseInfo :goods = 'goods'></detail-baseInfo>
      <detail-shopInfo :shop = 'shop'></detail-shopInfo> 
      <detail-goods-info :detail-info = 'detailInfo' @detailImageLoad = 'detailImageLoad'></detail-goods-info>
      <detail-param-info :param-info = 'paramInfo' ref = "param"></detail-param-info>
      <detail-comment-info :comment-info= 'commentInfo' ref="comment" ></detail-comment-info>
      <goods-list :goods = 'recommendArray' ref = "recommend"> </goods-list>
    
    </scroll>
    <detail-bottom-bar @addToCart='addToCart' ></detail-bottom-bar>
    <back-top @click.native='backTop' v-show='isShowBackTop'></back-top>
  </div>
</template>

<script>
import DetailNavBar from './childComps/DetailNavBar'
import DetailSwiper from './childComps/DetailSwiper'
import DetailBaseInfo from './childComps/DetailBaseInfo'
import DetailShopInfo from './childComps/DetailShopInfo'
import DetailGoodsInfo from './childComps/DetailGoodsInfo'
import DetailParamInfo from './childComps/DetailParamInfo'
import DetailCommentInfo from './childComps/DetailCommentInfo'
import DetailBottomBar from './childComps/DetailBottomBar'
 


import Scroll from 'components/common/scroll/Scroll'
import GoodsList from 'components/content/goods/GoodsList'


import {getDetail,Goods,Shop,GoodsParam,getRecommend} from 'network/detail'
import {itemListenerMixin,backTopMixin} from 'common/mixin'
import {BACK_POSITION} from 'common/const'
import {debounce} from 'common/utils'
import { mapActions } from 'vuex'


export default {
  name: 'Detail',

  data() {
    return {
      iid: null,
      topImages: [],
      goods: {},
      shop: {},
      detailInfo: {},
      paramInfo: {},
      commentInfo: {},
      recommendArray: [],
      themeTopYs: [],
      getThemeTopY: null,
      currentIndex: 0

    }
  },

  components: {
    DetailNavBar,
    Scroll,
    DetailSwiper,
    DetailBaseInfo,
    DetailShopInfo,
    DetailGoodsInfo,
    DetailParamInfo,
    DetailCommentInfo,
    GoodsList,
    DetailBottomBar
  },

  mixins: [itemListenerMixin,backTopMixin],
  created() {
    this.iid = this.$route.params.iid
    getDetail(this.iid).then(res =>{
       const data = res.result
       this.topImages = data.itemInfo.topImages
       this.goods = new Goods(data.itemInfo,data.columns,data.shopInfo.services)
       this.shop =  new Shop(data.shopInfo)
       this.detailInfo = data.detailInfo
       this.paramInfo = new GoodsParam(data.itemParams.info,data.itemParams.rule)
       if (data.rate.list) {
          this.commentInfo = data.rate.list[0];
        }
    })
    getRecommend().then(res => {
      this.recommendArray = res.data.list
    })
    //创建变量接收函数返回值
    this.getThemeTopY = debounce(() => {
      this.themeTopYs = []
      this.themeTopYs.push(0);
      this.themeTopYs.push(this.$refs.param.$el.offsetTop)
      this.themeTopYs.push(this.$refs.comment.$el.offsetTop)
      this.themeTopYs.push(this.$refs.recommend.$el.offsetTop)
    })
  },
  methods: {
    ...mapActions(['addCart']),  //映射action中的方法
    detailImageLoad() {
      this.newRefresh()
      this.getThemeTopY()
    },

    titleClick(index) {
      this.$refs.scroll.scrollTo(0,-this.themeTopYs[index],200)
    },
    
    contentScroll(position) {
      //获取Y值
      const positionY = -position.y
      //判断Y值
      let length = this.themeTopYs.length
      for(let i = 0; i < length; i++){
        if(this.currentIndex !=i && (i < length - 1 && positionY >= this.themeTopYs[i] && positionY < this.themeTopYs[i+1]) || (i == length - 1 && positionY >= this.themeTopYs[i])){
         // console.log("-------------"+i)
          this.currentIndex = i 
          this.$refs.nav.currentIndex  =  this.currentIndex
        }
      }
      this.isShowBackTop = -position.y > BACK_POSITION
    },
    addToCart() {
      //获取购物购物车需要展示的信息
      const product = {}
      product.iid = this.iid
      product.image = this.topImages[0]
      product.title = this.goods.title
      product.desc = this.goods.desc
      product.price = this.goods.realPrice
      console.log(product); 
      //方式一
      // this.$store.dispatch('addCart',product).then(res => {
      //   console.log(res)
      // })
      this.addCart(product).then(res => {
        console.log('------'+res)
        this.$toast.show(res,2000)
      })
      
      
    }
  }
}
</script>
<style scoped>
  #detail {
    height: 100vh;
    position: relative;
    z-index: 9;
    background-color: #fff;
  }

  .detail-nav {
    position: relative;
    z-index: 9;
    background-color: #fff;   
  }
  .content {
     height: calc(100% - 102px);
  }



</style>